﻿namespace CloudSqlService_WebRole
{
    using System;
    using System.Data.Services.Client;
    using System.Linq;
    using CloudSql.Entities;
    using Microsoft.Samples.ServiceHosting.StorageClient;

    /// <summary>
    /// Default page code-behind
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
        /// <summary>
        /// Web page load
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The eventargs
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string statusMessage = String.Empty;
            try
            {
                StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");

                // dynamically create the tables
                TableStorage.CreateTablesFromModel(typeof(AuditRowEntityDataServiceContext), accountInfo);
                TableStorage.CreateTablesFromModel(typeof(AuditRowIndexEntityDataServiceContext), accountInfo);

                AuditRowEntityDataServiceContext context = new AuditRowEntityDataServiceContext(accountInfo);
                this.auditRowList.DataSource = context.AuditRows.Take(10);
                this.auditRowList.DataBind();
            }
            catch (DataServiceRequestException ex)
            {
                statusMessage = "Unable to connect to the table storage server. Please check that the service is running.<br>"
                                + ex.Message + ex.InnerException.Message;
            }

            status.Text = statusMessage;

        }

        /// <summary>
        /// This test event response method adds data to the auditrows table
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The eventargs
        /// </param>
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
            AuditRowEntityDataServiceContext context = new AuditRowEntityDataServiceContext(accountInfo);
            context.AddAuditRowEntity(this.partitionKeyBox.Text, this.rowKeyBox.Text, this.dataBox.Text, this.displayDataBox.Text);
        }

        /// <summary>
        /// This event response method is to test the find LINQ... which turned otu to be a bit tricky
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The eventargs
        /// </param>
        protected void FindButton_Click(object sender, EventArgs e)
        {
            StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
            var auditRowIndexEntityDataServiceContext = new AuditRowIndexEntityDataServiceContext(accountInfo);
            if (auditRowIndexEntityDataServiceContext.Exists(this.rowKeyBox.Text))
            {
                this.displayDataBox.Text = auditRowIndexEntityDataServiceContext.NextSeqForRow(this.rowKeyBox.Text, this.partitionKeyBox.Text).ToString();
            }
            else
            {
                auditRowIndexEntityDataServiceContext.AddAuditRowIndexEntity(this.rowKeyBox.Text, 1, this.partitionKeyBox.Text);
                this.displayDataBox.Text = "1";
            }
        }

    }
}
